import {routerStatus} from '@/pinia'
import {watch} from 'vue'

/**
 * loading 加载状态
 * @param text 加载文案
 */
export const loading = (text: string = '加载中') => {
    const body = document.body
    const div = document.createElement('div')
    const textContent = document.createElement('div')
    textContent.className = 'content'
    textContent.textContent = text
    div.className = 'loading'
    div.append(textContent)
    body.appendChild(div)

    const store = routerStatus()

    // 监听 pinia 状态
    watch(() => store.loading, (val) => {
        if (!val && body.contains(div)) {
            body.removeChild(div)
        }
    }, { immediate: true })
}
